package com.hctforgreen.greenservice.ui.adapter;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.StartPwdActivity;
import com.hctforgreen.greenservice.ctr.DbController;
import com.hctforgreen.greenservice.ctr.HctController;
import com.hctforgreen.greenservice.db.DBManager;
import com.hctforgreen.greenservice.model.PwdEntity;
import com.hctforgreen.greenservice.model.SpareBootPasswordEntity;
import com.hctforgreen.greenservice.model.MachineListEntity.MachineEntity;
import com.hctforgreen.greenservice.model.SerieListEntity.SerieEntity;
import com.hctforgreen.greenservice.utils.HctConstants;
import com.hctforgreen.greenservice.utils.HctResult;
import com.hctforgreen.greenservice.utils.LoginResultStoreUtil;

/**
 * 历史密码记录
 * 
 * @author ld 20131122
 * 
 */
@SuppressLint("ResourceAsColor")
public class HisPwdListAdapter extends BaseAdapter {
	private List<PwdEntity> pwd_list = new ArrayList<PwdEntity>();
	StartPwdActivity mStartPwdActivity;
	private LayoutInflater mInflater;
	private Context context;
	private OnClickListener click;
	private DBManager mgr;
	public boolean isEdit = false;
	private String MachineModel;//手机型号

	public HisPwdListAdapter(Context context, List<PwdEntity> pwd_list,
			OnClickListener click, DBManager mgr) {
		super();
		// TODO Auto-generated constructor stub
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
		this.pwd_list = pwd_list;
		this.click = click;
		this.mgr = mgr;
		mStartPwdActivity = new StartPwdActivity();
		this.MachineModel = android.os.Build.MODEL;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return pwd_list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@SuppressWarnings("deprecation")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_his_list_view, null);
			holder.date_txt = (TextView) convertView
					.findViewById(R.id.date_txt);

			// holder.link_backuppwd = (TextView)
			// convertView.findViewById(R.id.link_backuppwd1);
			holder.link_backuppwd = (TextView) convertView
					.findViewById(R.id.link_backuppwd);
			holder.link_backuppwd.setOnClickListener(click);

			holder.del_btn = (Button) convertView.findViewById(R.id.del_btn);
			holder.del_btn.setOnClickListener(click);

			holder.bigUnit_complete_btn = (Button) convertView
					.findViewById(R.id.bigUnit_complete_btn);
			holder.bigUnit_complete_btn.setOnClickListener(click);

			holder.mark_txt = (TextView) convertView
					.findViewById(R.id.mark_txt);
			holder.pro_name_txt = (TextView) convertView
					.findViewById(R.id.pro_name_txt);
			holder.owner_name_txt = (TextView) convertView
					.findViewById(R.id.owner_name_txt);
			holder.qr_code_txt = (TextView) convertView
					.findViewById(R.id.qr_code_txt);
			holder.pwd_txt = (TextView) convertView.findViewById(R.id.pwd_txt);

			holder.owner_phone_txt = (TextView) convertView
					.findViewById(R.id.tv_owner_phone);

			holder.industry_txt = (TextView) convertView
					.findViewById(R.id.tv_industry);
			holder.backuppwd_txt = (TextView) convertView
					.findViewById(R.id.backuppwd_txt);
			holder.bigUnit_refuseReson_txt = (TextView) convertView
					.findViewById(R.id.bigUnit_refuseReson_txt);
			holder.lyt_bigUnit_refuseReson = (LinearLayout) convertView
					.findViewById(R.id.lyt_bigUnit_refuseReson);

			holder.loadPb = (ProgressBar) convertView
					.findViewById(R.id.pb_loading);
			holder.button_sentPassword = (Button) convertView.findViewById(R.id.button_sentPassword);
			holder.button_sentPassword.setOnClickListener(click);
			holder.text_sentRemark = (TextView) convertView.findViewById(R.id.text_sentRemark);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		/*
		 *  对按钮的点击事件设置回调参数（调用父activity是需设置，否则无法定位哪个tag下的按钮）
		 */
		holder.del_btn.setTag(position);
		holder.bigUnit_complete_btn.setTag(position);
		holder.link_backuppwd.setTag(position);
		holder.button_sentPassword.setTag(position);

		/*
		 * 对各组件赋值
		 */
		final PwdEntity entity = pwd_list.get(position);
		String phone = LoginResultStoreUtil.get(context).phone;
		entity.phone = phone;
		holder.date_txt.setText(entity.date);
		holder.mark_txt.setText("备注：" + entity.note);
		holder.pro_name_txt.setText("工程名：" + entity.projectName);
		holder.owner_name_txt.setText("业主姓名：" + entity.ownerName);
		holder.qr_code_txt.setText("条码：" + entity.stripeCode);
		holder.owner_phone_txt.setText("联系电话：" + entity.ownerPhone);
		holder.industry_txt.setText("安装行业：" + entity.industry);

		String sparebootPwd = entity.sparebootPwd;
		
		/*
		 *大机组各组件展示 
		 */
		if (isBigUnit(entity.stripeCode)) {
			holder.link_backuppwd.setVisibility(View.GONE);//获取备用密码
			holder.bigUnit_complete_btn.setVisibility(View.GONE);//大机组包完成
			holder.lyt_bigUnit_refuseReson.setVisibility(View.GONE);//大机组拒绝原因
			holder.button_sentPassword.setVisibility(View.GONE);//红外发送按钮
			holder.text_sentRemark.setVisibility(View.GONE);//红外发送备注
			
			if (entity.verifyStatus.equals("0")) {
				holder.backuppwd_txt.setText("");
				holder.pwd_txt.setText("密码："
						+ context.getResources().getString(
								R.string.bigunit_password_appling));
			} else if (entity.verifyStatus.equals("1")) {
				holder.bigUnit_complete_btn.setVisibility(View.VISIBLE);
				holder.link_backuppwd.setVisibility(View.VISIBLE);
				/*
				 * 非格力手机，不显示红外发送
				 */
				if(MachineModel.equals("G0111")||MachineModel.equals("G01111")
						|| MachineModel.equals("G0215D") || MachineModel.equals("G0235D")){
					holder.button_sentPassword.setVisibility(View.VISIBLE);
					holder.text_sentRemark.setVisibility(View.VISIBLE);
				}else{
					holder.button_sentPassword.setVisibility(View.GONE);
					holder.text_sentRemark.setVisibility(View.GONE);
				}
				
				holder.pwd_txt.setText("密码：" + entity.openPassword);
				holder.backuppwd_txt.setText("");
				holder.link_backuppwd.setClickable(true);
				holder.bigUnit_complete_btn.setClickable(true);
				holder.bigUnit_complete_btn.setText("调试报完成");
				holder.bigUnit_complete_btn.setBackgroundDrawable(context
						.getResources().getDrawable(R.drawable.btn_orange));
			} else if (entity.verifyStatus.equals("2")) {
				holder.lyt_bigUnit_refuseReson.setVisibility(View.VISIBLE);
				
				holder.pwd_txt.setText("密码：" + entity.openPassword);
				holder.backuppwd_txt.setText("");
				holder.bigUnit_refuseReson_txt.setText("拒绝理由："
						+ entity.refuseReason);
				holder.bigUnit_refuseReson_txt.setTextColor(context
						.getResources().getColor(R.color.red));
			} else if (entity.verifyStatus.equals("3")) {
				holder.bigUnit_complete_btn.setVisibility(View.VISIBLE);
				holder.link_backuppwd.setVisibility(View.VISIBLE);
				/*
				 * 非格力手机，不显示红外发送
				 */
				if(MachineModel.equals("G0111")||MachineModel.equals("G01111")
						|| MachineModel.equals("G0215D") || MachineModel.equals("G0235D")){
					holder.button_sentPassword.setVisibility(View.VISIBLE);
					holder.text_sentRemark.setVisibility(View.VISIBLE);
				}else{
					holder.button_sentPassword.setVisibility(View.GONE);
					holder.text_sentRemark.setVisibility(View.GONE);
				}
				
				holder.pwd_txt.setText("密码：" + entity.openPassword);
				holder.bigUnit_complete_btn.setClickable(false);
				holder.link_backuppwd.setClickable(true);
				holder.backuppwd_txt.setText("");
				holder.bigUnit_complete_btn.setText("调试已完成");
				holder.bigUnit_complete_btn
						.setBackgroundDrawable(context.getResources()
								.getDrawable(R.drawable.ic_gray_rect_bg));
			}
			
			if (!sparebootPwd.equals("")) {
				holder.backuppwd_txt.setText("备用密码：" + sparebootPwd);
				holder.link_backuppwd.setText("");
				holder.link_backuppwd.setClickable(false);
			} else {
				holder.backuppwd_txt.setText("");
				holder.link_backuppwd.setText(R.string.backuppwd);
				holder.link_backuppwd.setClickable(true);
			}

		} else {
			/*
			 * 非格力手机，不显示红外发送
			 */
			if(MachineModel.equals("G0111")||MachineModel.equals("G01111")
					|| MachineModel.equals("G0215D") || MachineModel.equals("G0235D")){
				holder.button_sentPassword.setVisibility(View.VISIBLE);
				holder.text_sentRemark.setVisibility(View.VISIBLE);
			}else{
				holder.button_sentPassword.setVisibility(View.GONE);
				holder.text_sentRemark.setVisibility(View.GONE);
			}
			
			holder.lyt_bigUnit_refuseReson.setVisibility(View.GONE);
			holder.bigUnit_complete_btn.setVisibility(View.GONE);
			holder.pwd_txt.setText("密码：" + entity.openPassword);
			holder.link_backuppwd.setVisibility(View.VISIBLE);
			if (!sparebootPwd.equals("")) {
				holder.backuppwd_txt.setText("备用密码：" + sparebootPwd);
				holder.link_backuppwd.setText("");
				holder.link_backuppwd.setClickable(false);
			} else {
				holder.backuppwd_txt.setText("");
				holder.link_backuppwd.setText(R.string.backuppwd);
				holder.link_backuppwd.setClickable(true);
			}
			
			
		}
		
		
		
		
		return convertView;
	}

	/**
	 * 
	 * 是否是大机组
	 */
	public boolean isBigUnit(String lBarcode) {
		// Log.i("DATA", "isBigUnit----1---" + lBarcode);

		if (lBarcode == null || lBarcode.length() < 5) {
			return false;
		}
		DbController controller = new DbController(((Activity) context));
		// DbController controller = new DbController(context);
		MachineEntity lEntity = controller.getMachineEntity(lBarcode.substring(
				0, 5));
		if (lEntity == null || lEntity.seriesId.replace(" ", "").length() < 1) {
			return false;
		}

		SerieEntity serieEntity = controller.getSerieEntity(lEntity.seriesId);
		if (serieEntity != null && serieEntity.seriesTypeName != null) {
			return serieEntity.seriesTypeName.equals("largeUnit_machine");
		}

		// if (lBarcode.substring(0, 5).equals("95111")) { return true; }

		return false;

	}

	static class ViewHolder {
		TextView date_txt;
		Button del_btn;
		TextView mark_txt;
		TextView pro_name_txt;
		TextView owner_name_txt;
		TextView qr_code_txt;
		TextView pwd_txt;
		TextView owner_phone_txt;
		TextView industry_txt;

		TextView backuppwd_txt;
		TextView link_backuppwd;
		ProgressBar loadPb;
		LinearLayout lyt_bigUnit_refuseReson;
		TextView bigUnit_refuseReson_txt;
		Button bigUnit_complete_btn;
		Button button_sentPassword;
		TextView text_sentRemark;
	}

}
